package com.hm.ibsm.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hm.ibsm.manage.vo.user.UserVo;
import com.hm.ibsm.service.UserService;
import com.hm.ibsm.util.BaseController;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

/**
 * @author shishun.wang
 * @date 2017年3月9日 上午10:20:34
 * @version 1.0
 * @describe
 */
@Slf4j
@Controller
@RequestMapping("/mg/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	private String MODEL_NAME = UserController.class.getName();

	@RequestMapping(value = "/index.do", method = RequestMethod.GET)
	public String userMg() {
		return "mg/user";
	}

	@RequestMapping(value = "/add.do", method = RequestMethod.GET)
	public String addUser() {
		try {
			return "mg/user/add";
		} catch (Exception e) {
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/update.do", method = RequestMethod.GET)
	public ModelAndView updateUser() {
		try {
			return new ModelAndView("mg/user/update");
		} catch (Exception e) {
			return sessionTimeOut();
		}
	}

	@RequestMapping(value = "/addUser.do", method = RequestMethod.POST)
	public String saveAddUser(UserVo user) {
		try {
			return userMg();
		} catch (Exception e) {
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/updateUser.do", method = RequestMethod.POST)
	public String saveUpdateUser(UserVo user) {
		try {
			return userMg();
		} catch (Exception e) {
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/deleteUser.do", method = RequestMethod.GET)
	public String deleteUser(UserVo user) {
		try {
			return userMg();
		} catch (Exception e) {
			return SESSION_TIME_OUT_URL;
		}
	}

	@RequestMapping(value = "/query.do", method = RequestMethod.GET)
	public @ResponseBody String query() {
		try {
			return JSONObject.fromObject(userService.query(1, 10)).toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
